-- More Pickables Mod
-------------------------------------------------------------------------------
-- Description:
--
-- Increases number of drops provided by pickable items including grass,
-- saplings, reeds and berry bushes.
--
-------------------------------------------------------------------------------
-- Features:
--
-- * Drops 3 cutgrass for grasses when picked and 2 cutgrass when dug up.
--   
-- * Drops 3 twigs for saplings when picked and 2 twigs when dug up.
--
-- * Drops 3 cutreeds for reeds when picked and 2 cutreeeds when dug up.
--
-- * Drops 3 berries for berry bushes when picked and 2 berries when dug up.
--
-------------------------------------------------------------------------------
-- Compatibility:
--
-- Don't Starve Revision 74452 2013-03-12_14-18-42 (A Little Rain Must Fall)
--
-- -no known incompatibilites-
--
-------------------------------------------------------------------------------
-- Changelog:
--
-- v1.1
-- Added berry bushes
-- Revised by WrathOf
--
-- v1.0
-- Added reeds
--
-- v0.5
-- Initial release for Grass and Saplings
--
-------------------------------------------------------------------------------
-- Support:
--
-- Report issues found or suggestions to this thread on Klei's forums
-- http://forums.kleientertainment.com/showthread.php?12091
--
-------------------------------------------------------------------------------
--
-- By Alpaca, Starving
-- Reviewed and updated by WrathOf
-- Version 1.1
-- Updated: 2013-03-23
--
-------------------------------------------------------------------------------
print("Loading More Pickables Mod....")


--Localize commonly used globals
SpawnPrefab = GLOBAL.SpawnPrefab
Vector3	= GLOBAL.Vector3
TheSim = GLOBAL.TheSim


--
-- Cut Grass
--

local function NEW_grass_onpickedfn(inst, picker)

	--Run standard function previously saved to handle the animations, etc
	if inst.ASMP_OnPickedFn then
		inst.ASMP_OnPickedFn(inst, picker)
	end

	--Drop additional loot versus just the one cutgrass provided by the pickable component
	if picker and picker.components.inventory then
		local pt = Vector3( TheSim:GetScreenPos( inst.Transform:GetWorldPosition() ) )
		picker.components.inventory:GiveItem(SpawnPrefab("cutgrass"), nil, pt)
		picker.components.inventory:GiveItem(SpawnPrefab("cutgrass"), nil, pt)
	end

end

local function NEW_grass_dig_up(inst, chopper)
	--Modified game code (different for each prefab)
	--NOTE: must be reviewed after game updates to ensure compatible
	if inst.components.pickable and inst.components.pickable:CanBePicked() and inst.components.lootdropper then
		inst.components.lootdropper:SpawnLootPrefab("cutgrass")
		inst.components.lootdropper:SpawnLootPrefab("cutgrass")
	end
	inst:Remove()
	if inst.components.lootdropper then
		inst.components.lootdropper:SpawnLootPrefab("dug_grass")
	end
end

local function grassPrefabPostInit(inst)

	if inst then
		if inst.components.pickable then
			--Save existing function to run from our new version
			inst.ASMP_OnPickedFn = inst.components.pickable.onpickedfn
			--Replace Pickable component's onpickedfn setting with our new one
			inst.components.pickable.onpickedfn = NEW_grass_onpickedfn
		end

		--Replace Workable component's OnFinishCallback setting with our new one
		if inst.components.workable then
			inst.components.workable:SetOnFinishCallback(NEW_grass_dig_up)
		end
	end

end

AddPrefabPostInit("grass", grassPrefabPostInit)


--
-- Saplings
--

local function NEW_sapling_onpickedfn(inst, picker)

	--Run standard function previously saved to handle the animations, etc
	if inst.ASMP_OnPickedFn then
		inst.ASMP_OnPickedFn(inst, picker)
	end

	--Drop additional loot versus just the one twigs provided by the pickable component
	if picker and picker.components.inventory then
		local pt = Vector3( TheSim:GetScreenPos( inst.Transform:GetWorldPosition() ) )
		picker.components.inventory:GiveItem(SpawnPrefab("twigs"), nil, pt)
		picker.components.inventory:GiveItem(SpawnPrefab("twigs"), nil, pt)
	end

end

local function NEW_sapling_dig_up(inst, chopper)
	--Modified game code (different for each prefab)
	--NOTE: must be reviewed after game updates to ensure compatible
	if inst.components.pickable and inst.components.pickable:CanBePicked() and inst.components.lootdropper then
		inst.components.lootdropper:SpawnLootPrefab("twigs")
		--Added 1 more berries to drop
		inst.components.lootdropper:SpawnLootPrefab("twigs")
	end
	inst:Remove()
	if inst.components.lootdropper then
		inst.components.lootdropper:SpawnLootPrefab("dug_sapling")
	end
end

local function saplingPrefabPostInit(inst)

	if inst then
		if inst.components.pickable then
			--Save existing function to run from our new version
			inst.ASMP_OnPickedFn = inst.components.pickable.onpickedfn
			--Replace Pickable component's onpickedfn setting with our new one
			inst.components.pickable.onpickedfn = NEW_sapling_onpickedfn
		end

		--Replace Workable component's OnFinishCallback setting with our new one
		if inst.components.workable then
			inst.components.workable:SetOnFinishCallback(NEW_sapling_dig_up)
		end
	end

end

AddPrefabPostInit("sapling", saplingPrefabPostInit)


--
-- Reeds
--

local function NEW_reeds_onpickedfn(inst, picker)

	--Run standard function previously saved to handle the animations, etc
	if inst.ASMP_OnPickedFn then
		inst.ASMP_OnPickedFn(inst, picker)
	end

	--Drop additional loot versus just the one cutreeds provided by the pickable component
	if picker and picker.components.inventory then
		local pt = Vector3( GLOBAL.TheSim:GetScreenPos( inst.Transform:GetWorldPosition() ) )
		picker.components.inventory:GiveItem(SpawnPrefab("cutreeds"), nil, pt)
		picker.components.inventory:GiveItem(SpawnPrefab("cutreeds"), nil, pt)
	end

end

local function reedsPrefabPostInit(inst)

	if inst and inst.components.pickable then
		--Save existing function to run from our new version
		inst.ASMP_OnPickedFn = inst.components.pickable.onpickedfn
		--Replace Pickable component's onpickedfn setting with our new one
		inst.components.pickable.onpickedfn = NEW_reeds_onpickedfn
	end

end

AddPrefabPostInit("reeds", reedsPrefabPostInit)


--
-- Berry bushes
--

local function NEW_berrybush_onpickedfn(inst, picker)

	--Run standard function previously saved to handle the animations, etc
	if inst.ASMP_OnPickedFn then
		inst.ASMP_OnPickedFn(inst, picker)
	end

	--Drop additional loot versus just the one berries provided by the pickable component
	if picker and picker.components.inventory then
		local pt = Vector3( TheSim:GetScreenPos( inst.Transform:GetWorldPosition() ) )
		picker.components.inventory:GiveItem(SpawnPrefab("berries"), nil, pt)
		picker.components.inventory:GiveItem(SpawnPrefab("berries"), nil, pt)
	end

end

local function NEW_berrybush_dig_up(inst, chopper)
	--Modified game code (different for each prefab)
	--NOTE: must be reviewed after game updates to ensure compatible
	inst:Remove()
	if inst.components.pickable and inst.components.lootdropper then

		if inst.components.pickable:IsBarren() then
			inst.components.lootdropper:SpawnLootPrefab("twigs")
			inst.components.lootdropper:SpawnLootPrefab("twigs")
		else

			if inst.components.pickable and inst.components.pickable:CanBePicked() then
				inst.components.lootdropper:SpawnLootPrefab("berries")
				--Added 1 more berries to drop
				inst.components.lootdropper:SpawnLootPrefab("berries")
			end
			--Use prefab name since there are more than 1 variety of berry bush
			inst.components.lootdropper:SpawnLootPrefab("dug_"..inst.prefab)
		end
	end	
end

local function berrybushPrefabPostInit(inst)

	if inst then
		if inst.components.pickable then
			--Save existing function to run from our new version
			inst.ASMP_OnPickedFn = inst.components.pickable.onpickedfn
			--Replace Pickable component's onpickedfn setting with our new one
			inst.components.pickable.onpickedfn = NEW_berrybush_onpickedfn
		end

		--Replace Workable component's OnFinishCallback setting with our new one
		if inst.components.workable then
			inst.components.workable:SetOnFinishCallback(NEW_berrybush_dig_up)
		end
	end

end

AddPrefabPostInit("berrybush", berrybushPrefabPostInit)
AddPrefabPostInit("berrybush2", berrybushPrefabPostInit)

